<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("../_inc/procFunction.php");

$errMsg = "";
$module = sxREQ('mod');

if($module=='') {
	$url = $_SESSION[USEROBJ]->getHistory(0, true).
		"&msg=".urlencode(_t("missing mod '$module'"));
	serverRedir($url);
}

if(sxREQ("reqtype") == "save"){
	$errMsg .= crulIns();
}

$aOpz = array("kMenu"=>'pro.cru', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function objFrmSubm(rt){
	document.objFrm.reqtype.value = rt;
	document.objFrm.submit();
}
</script>
<?php
echo(bodyHTML($aOpz));

echo getModHeader($module);

$aCmd = array("save"=>array("pag"=>"javascript:objFrmSubm('save')",
										"title"=>_t("save"),
										"text"=>_t("save")
										),
					"back"=>array()
					);
$sCmd = commandsHTML($aCmd);	
echo $sCmd;

echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('reqtype','...');
echo getHtmlHidden('mod');

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='3'>Common Rule</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width=\"20%\">Name*</td>" .
			"<td class=\"text01\" width=\"80%\" colspan='2'>".getHtmlText('name',32,32)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Description*</td>" .
			"<td class=\"text01\" colspan='2'>".getHtmlText('description',80,80)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Subroutine allowed</td>" .
			"<td class=\"text01\" colspan='2'>".getHTMLRadio('admitted_sub', array('N'=>'no','Y'=>'yes'))."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Upgrade status</td>" .
			"<td class=\"text01\" colspan='2'>".getHTMLRadio('upgrade_status', getUpgradeStatusVal())."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Note</td>" .
			"<td class=\"text01\" colspan='2'>".getHtmlTextArea('note',80,2)."</td>" .
		"</tr>\n");
echo("</table>");
echo $sCmd;
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function crulIns(){
	global $objSDAO;
	$errMsg = "";
	if(sxRTrim("mod") == '')
		$errMsg .= "missing module<br>";
	if(sxRTrim("name") == '')
		$errMsg .= "missing name<br>";
	if(preg_match("/[^a-zA-Z0-9_]/",sxREQ("name")))
		$errMsg .= "name: only letter, nummber and _ allowed<br>";
	if(sxRTrim("description") == '')
		$errMsg .= "missing description<br>";
	//existence
	$sSQL = "SELECT name FROM com_rule "
		." WHERE name='".sxRAS("name")."'" 
		." AND module='".sxRAS("mod")."'";
	$objSDAO->query($sSQL,"test");
	if($row=$objSDAO->fetch('test'))
		$errMsg .= "existing name<br>";
		
	if($errMsg != ""){return $errMsg;}		
	
	$sSQL = "INSERT INTO " .
			"com_rule " .
			"(name, " .
			"module, " .
			"description, " .
			"admitted_sub, " .
			"upgrade_status, " .
			"note)" .
			"VALUES('".sxRAS("name")."', " .
			"'".sxRAS("mod")."', " .
			"'".sxRAS("description")."', " .
			"'".sxRAS("admitted_sub")."', " .
			"'".sxRAS("upgrade_status")."', " .
			"'".sxRAS("note")."'" .
			")";	
	$objSDAO->EXEC($sSQL);
	$url = "crulView.php?mod=".sxREQ("mod")."&name=".sxREQ("name");
	serverRedir($url);
	return 'redir err';
}
